from django.contrib import auth
from django.core.paginator import Paginator
from rest_framework.authtoken.models import Token
from backend.models import Performance, User, Project, Service, Research, Argument, \
    Achievement
from .util import *
from ..model.user import *
from ..model.project import *
from ..model.argument import *


def index(request):
    users = get_all_head_user()
    user_ids = []
    for u in users:
        user_ids.append(u.get('head_user'))
    if len(user_ids) == 0:
        return format_response(SUCCESS, [])
    user_ids = list(set(user_ids))
    head_data = []
    for user_id in user_ids:
        extra, project_list = get_projects_head_user_detail(user_id)
        user = User.objects.get(id=user_id)
        data = {
            "head_user": user.get_full_name(),
            "all_score": extra.get('all_score'),
            "all_num": extra.get('all_num'),
            "rank": 0,  # TODO
            "list": project_list
        }
        head_data.append(data)
    return format_response(SUCCESS, head_data)


def performance(request):
    users = get_all_head_user()
    user_ids = []
    for u in users:
        user_ids.append(u.get('head_user'))
    if len(user_ids) == 0:
        return format_response(SUCCESS, [])
    user_ids = list(set(user_ids))
    head_data = []
    for user_id in user_ids:
        extra, project_list = get_projects_head_user_detail(user_id, performance=True)
        user = User.objects.get(id=user_id)
        data = {
            "head_user": user.get_full_name(),
            "performance": extra.get('performance'),
            "rank": 0,  # TODO
            "list": project_list
        }
        head_data.append(data)
    return format_response(SUCCESS, head_data)


def achievement(request):
    users = get_all_head_user()
    user_ids = []
    for u in users:
        user_ids.append(u.get('head_user'))
    if len(user_ids) == 0:
        return format_response(SUCCESS, [])
    user_ids = list(set(user_ids))
    head_data = []
    for user_id in user_ids:
        extra, project_list = get_projects_head_user_detail(user_id, achievement=True)
        user = User.objects.get(id=user_id)
        data = {
            "head_user": user.get_full_name(),
            "achievement": extra.get('achievement'),
            "rank": 0,  # TODO
            "list": project_list
        }
        head_data.append(data)
    return format_response(SUCCESS, head_data)


def argument(request):
    page = request.GET.get('page')
    size = request.GET.get('size')

    arguments = get_all_argument()
    if len(arguments) == 0:
        return format_response(SUCCESS, [])

    total_page = 1
    if page and size:
        paginator = Paginator(arguments, size)
        total_page = paginator.num_pages
        arguments = paginator.page(page)

    data = []
    for item in arguments:
        data.append({
            'title': item.project.title,
            'head_user': item.project.head_user.get_full_name(),
            'serial': item.project.serial,
            'decree': item.project.decree,
            'total_number': item.project.total_number,
            'argument': item.total_num,
            'rank': 0,  # TODO
            'guide': item.guide,
            'standard': item.standard,
            'application': item.application
        })
    result = {
        "list": data,
        "total_page": total_page,
        "current_page": page
    }
    return format_response(SUCCESS, result)
